package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.dto.PasswordEditDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Api(tags = "员工信息处理层")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @ApiOperation(value = "登录")
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation("员工退出")
    public Result<String> logout() {
        return Result.success();
    }

    /**
     * 新建员工
     * @param employeeDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新建员工")
    public Result<String> addEmp(@RequestBody EmployeeDTO employeeDTO){
        log.info("新建员工{}",employeeDTO);
       employeeService.addEmp(employeeDTO);
       return Result.success();
    }

    /**
     * 查询员工 （支持名字模糊查询，和不带条件查询）
     * @param employeePageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("查询员工 （支持名字模糊查询，和不带条件查询）")
    public Result<PageResult> queryAllEmps(EmployeePageQueryDTO employeePageQueryDTO){
        PageResult pageResult = employeeService.queryAllEmps(employeePageQueryDTO);
        log.info("查询所有员工中");
        if (pageResult != null){
            return Result.success(pageResult);
        }else{
            return Result.error("创建失败");
        }
    }

    /**
     * 修改员工状态（1：启用 0: 禁用）
     */
    @PostMapping("status/{status}")
    @ApiOperation("修改员工装填（1：启用 0: 禁用）")
    public Result<String> setEmpState(@RequestParam Long id,@PathVariable Integer status){
            employeeService.setEmpState(id,status);
            return Result.success();

    }

    /**
     * 根据id查询员工信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询员工信息")
    public Result<Employee> getEmpById(@PathVariable Long id){
        return Result.success(employeeService.getEmpById(id));
    }

    /**
     * 更新员工数据
     * @param employeeDTO
     * @return
     */
    @PutMapping()
    @ApiOperation("更新员工数据")
    public Result<Object> updateEmp(@RequestBody EmployeeDTO employeeDTO){
        log.info(employeeDTO.toString());
        employeeService.updateEmp(employeeDTO);
        return Result.success();
    }

    /**
     * 修改密码
     * @param passwordEditDTO
     * @return
     */
    @PutMapping("/editPassword")
    @ApiOperation("修改密码")
    public Result<Object> updateEmpPwd(@RequestBody PasswordEditDTO passwordEditDTO){
        employeeService.updateEmpPwd(passwordEditDTO);

        return Result.success();
    }

}

